﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ViCode_LeVi.Utilities;
using ViCode_LeVi.Data;

namespace ViCode_LeVi.Controls
{
    /// <summary>
    /// Interaction logic for VMTabTaskItemGroup.xaml
    /// </summary>
    public partial class VMTabTaskItemGroup : UserControl
    {
        public VMTabTaskItemGroup()
        {
            InitializeComponent();
            new PropertyChangeNotifier(this, FrameworkElement.DataContextProperty, (s, e) =>
            {
                TabControl_QuestionAnswer_CreateTabItem();
            });
            this.DataContextChanged += new DependencyPropertyChangedEventHandler(VMTabTaskItemGroup_DataContextChanged);
        }

        void VMTabTaskItemGroup_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            TabControl_QuestionAnswer_CreateTabItem();
        }

        /// <summary>
        /// Cần 1 tab default de add them thong tin
        /// </summary>
        public void TabControl_QuestionAnswer_CreateTabItem()
        {
            var step_TaskItemStep =(Step_TaskItemStep) this.DataContext  ;
            Step_TaskItemGroup[] data = step_TaskItemStep== null? null: step_TaskItemStep.TaskItemGroups_Source.ToArray();
            if (data == null || data.Length == 0)
            {
                TabControl_TaskItemGroup.Items.Clear();
            }
            else
            {
                TabControl_TaskItemGroup.Items.Clear();
                for (int i = 0; i < data.Length; i++)
                {
                    Step_TaskItemGroup d = data[i];
                    TabItem item = new TabItem();// {  DataContext = d};
                    item.SetBinding(FrameworkElement.DataContextProperty, new Binding("DataContext.TaskItemGroups_Source[" + i.ToString() + "]")
                    {
                        RelativeSource = new RelativeSource
                            (RelativeSourceMode.FindAncestor
                            , typeof(VMTabTaskItemGroup)
                            , 1)
                    });

                    TabControl_TaskItemGroup.Items.Add(item);

                    //Header
                    StackPanel stackHeader = new StackPanel() {  Orientation = Orientation.Horizontal};
                    VMTextBlockExt headerName = new VMTextBlockExt();
                    headerName.TextPath = "Name";
                    stackHeader.Children.Add(headerName);
                    VMTextBlock header1 = new VMTextBlock() { Text = "(" };
                    stackHeader.Children.Add(header1);
                    VMTextBlockExt headerCode = new VMTextBlockExt();
                    headerCode.TextPath = "Code";
                    stackHeader.Children.Add(headerCode);
                    VMTextBlock header2 = new VMTextBlock() { Text = ")" };
                    stackHeader.Children.Add(header2);

                    item.Header = stackHeader;

                    //VMQuestionAnswer
                    VMTaskItemGroup vmqa = new VMTaskItemGroup();
                    vmqa.SetBinding(FrameworkElement.DataContextProperty, new Binding("."));
                    item.Content = vmqa;

                    d.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(d_PropertyChanged);
                }
                #region Summary
                #region sCodeSum
                var sCodeSubs = data.Select( c=> c.Code);
                StringBuilder sbCodeSum = new StringBuilder();
                foreach( var code in sCodeSubs)
                {
                    sbCodeSum .AppendFormat("{0}, " ,code);
                }
                string sCodeSum = sbCodeSum.ToString().Trim(' ',',');
                #endregion

                #region sum task
                List<Step_TaskItem> sumTasks = new List<Step_TaskItem>();
                foreach (var d in data)
                    sumTasks.AddRange(d.Tasks_Source.ToArray());                
                #endregion

                TabItem itemSum = new TabItem();
                itemSum.SetBinding(FrameworkElement.DataContextProperty, new Binding("DataContext")
                {
                    RelativeSource = new RelativeSource
                        (RelativeSourceMode.FindAncestor
                        , typeof(VMTabTaskItemGroup)
                        , 1)
                });
                TabControl_TaskItemGroup.Items.Add(itemSum);

                //Header
                //headerSum = new VMTextBlock() { Text = string.Format("Weighting ({0})", sCodeSum) };
                StackPanel headerEdit = new StackPanel() { Orientation = Orientation.Horizontal };
                headerEdit.Children.Add(new VMTextBlockExt_ControlTextInfoInData() { TextPath = "TaskItem_WeightText" });
                headerEdit.Children.Add(new VMTextBlock() { Text = "(" + sCodeSum + ")"});
                itemSum.Header = headerEdit;

                //VMQuestionAnswer
                VMTaskItemGroupSummary vmqaSum = new VMTaskItemGroupSummary();
                vmqaSum.SetBinding(FrameworkElement.DataContextProperty, new Binding("Tasks_Summary") { Mode = BindingMode.OneWay });
                itemSum.Content = vmqaSum;
                #endregion
                TabControl_TaskItemGroup.UpdateLayout();
            }
        }
        VMTextBlock headerSum;
        void d_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            var step_TaskItemStep = (Step_TaskItemStep)this.DataContext;
            Step_TaskItemGroup[] data = step_TaskItemStep == null ? null : step_TaskItemStep.TaskItemGroups_Source.ToArray();
            if (data == null || data.Length == 0)
            {
                TabControl_TaskItemGroup.Items.Clear();
            }
            if (headerSum != null)
            {
                #region sCodeSum
                var sCodeSubs = data.Select(c => c.Code);
                StringBuilder sbCodeSum = new StringBuilder();
                foreach (var code in sCodeSubs)
                {
                    sbCodeSum.AppendFormat("{0}, ", code);
                }
                string sCodeSum = sbCodeSum.ToString().Trim(' ', ',');
                #endregion

                headerSum.Text = string.Format("Weight ({0})", sCodeSum);
            }
        }

        #region IVMRefresh Members

        public void VMRefresh()
        {
            TabControl_QuestionAnswer_CreateTabItem();
        }

        #endregion
    }
}